package com.xiangxiao.rpan.storage.core;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/12/5 15:22
 */
@Component(value = "downloadFileToolSelector")
public class DownloadFileToolSelector implements ApplicationContextAware {
  private ApplicationContext applicationContext;

  private DownloadFileTool downloadFileTool;

  @Value("${rpan.download.selector}")
  private Class<? extends DownloadFileTool> downloadFileSelector;
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    if (Objects.isNull(downloadFileSelector)) {
      throw new RuntimeException("the download transfer selector can not be null!<rpan.download.transfer.selector>");
    }
    this.downloadFileTool = this.applicationContext.getBean(downloadFileSelector);
    if (Objects.isNull(this.downloadFileSelector)) {
      throw new RuntimeException("the storage download type:" + downloadFileSelector.getSimpleName() + " can not be found!");
    }
  }

  /**
   * 选择TransferFileByCacheTool实现类
   *
   * @return
   */
  public DownloadFileTool select() {
    return downloadFileTool;
  }
}
